package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.bean.StoreInfoBeen;
import cn.xfdzx.android.apps.shop.contract.StoreInfoContract;
import cn.xfdzx.android.apps.shop.model.StoreInfoModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class StoreInfoPresenter extends BasePresenter<StoreInfoContract.View> implements StoreInfoContract.Presenter {

    private StoreInfoContract.Model model;

    public StoreInfoPresenter() {
        model = new StoreInfoModel();
    }

    @Override
    public void initData(String store_id, String sales_model) {
        if (!isViewAttached()) {
            return;
        }
        model.initData( store_id,sales_model)
                .compose(RxScheduler.<StoreInfoBeen>Flo_io_main())
                .as(mView.<StoreInfoBeen>bindAutoDispose())
                .subscribe(new Consumer<StoreInfoBeen>() {
                    @Override
                    public void accept(StoreInfoBeen bean) throws Exception {
                        mView.onInitSuccess(bean);

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);

                    }
                });
    }

    @Override
    public void addCar(String is_new, String uniqueId, String productId, String cartNum,int position,int total_num) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.addCar(is_new,uniqueId,productId,cartNum) .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onAddCarSuccess(bean,  position,  total_num);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void initList(boolean isRefresh,boolean isLoading, String lng,  String lat,String sid, String page, String limit, String index_status, String is_store, String store_id, String keyword, String priceOrder, String salesOrder, String news) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.initList(lng,lat,sid, page, limit, index_status, is_store, store_id, keyword, priceOrder, salesOrder, news).compose(RxScheduler.<HomeSearchXFDGoodBeen>Flo_io_main())
                .as(mView.<HomeSearchXFDGoodBeen>bindAutoDispose()).subscribe(new Consumer<HomeSearchXFDGoodBeen>() {
            @Override
            public void accept(HomeSearchXFDGoodBeen baseObjectBean) throws Exception {
                mView.onListSuccess(baseObjectBean);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else{
                    mView.finishLoadMore();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else {
                    mView.finishLoadMore();
                }
            }
        });
    }

    @Override
    public void getFocusShop(String storeid) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getFocusShop(storeid)
                .compose(RxScheduler.<StoreInfoBeen>Flo_io_main())
                .as(mView.<StoreInfoBeen>bindAutoDispose())
                .subscribe(new Consumer<StoreInfoBeen>() {
                    @Override
                    public void accept(StoreInfoBeen bean) throws Exception {
                        mView.getFocusShopSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
}
